package sfmc.biz.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import sfmc.biz.service.SfmcCustomerService;
import sfmc.biz.vo.Sfmc_TB_CUSTOMER;
import sfmc.biz.vo.Sfmc_TB_CUSTSTATUS;

@Controller
public class SfmcCustomerController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Autowired 
	private SfmcCustomerService sfmcCustomerService;
	
	@RequestMapping(value ="/customer_list.do")
	public ModelAndView list(ModelAndView mv, HttpSession session, HttpServletRequest request, Sfmc_TB_CUSTOMER sfmc_TB_CUSTOMER){

		mv.setViewName("/customer/customer_list");
		
		if(sfmc_TB_CUSTOMER.getPageNo() == 0){
			sfmc_TB_CUSTOMER.setPageNo(1);
			sfmc_TB_CUSTOMER.setPageSize(20);
			sfmc_TB_CUSTOMER.setOffSet(0);
			sfmc_TB_CUSTOMER.setTotalCnt(0);
		}else{
			sfmc_TB_CUSTOMER.setOffSet((sfmc_TB_CUSTOMER.getPageNo()-1)*sfmc_TB_CUSTOMER.getPageSize());
			sfmc_TB_CUSTOMER.setTotalCnt(sfmcCustomerService.getCustomerCount(sfmc_TB_CUSTOMER));
		}
		List<Sfmc_TB_CUSTOMER> customerList = sfmcCustomerService.getCustomerList(sfmc_TB_CUSTOMER);
		
		mv.addObject("customerlist", (new Gson()).toJson(customerList));
		mv.addObject("customerdata", sfmc_TB_CUSTOMER);
		
		return mv;
	}
	
	@RequestMapping(value ="/customer_save.do" , method = RequestMethod.POST)
	public ModelAndView save(ModelAndView mv, HttpSession session, HttpServletRequest request, Sfmc_TB_CUSTOMER sfmc_TB_CUSTOMER){
		
		mv.setViewName("/customer/customer_result");

		int returnVal = -1;
		String type = "";
		
		if(sfmc_TB_CUSTOMER.getDataCustIdx() == 0){
			returnVal = sfmcCustomerService.insertCustomer(sfmc_TB_CUSTOMER);
			type = "C";
		}else{
			returnVal = sfmcCustomerService.updateCustomer(sfmc_TB_CUSTOMER);
			type = "U";
		}
		
		mv.addObject("customerdata", sfmc_TB_CUSTOMER);
		mv.addObject("returnVal", returnVal);
		mv.addObject("type", type);
		return mv;
	}
	
	@RequestMapping(value ="/customer_delete.do" , method = RequestMethod.POST)
	public ModelAndView delete(ModelAndView mv, HttpSession session, HttpServletRequest request, Sfmc_TB_CUSTOMER sfmc_TB_CUSTOMER){
		
		mv.setViewName("/customer/customer_result");

		int returnVal = -1;
		String type = "D";
		
		if(sfmc_TB_CUSTOMER.getDataCustIdx() != 0){
			returnVal = sfmcCustomerService.deleteCustomer(sfmc_TB_CUSTOMER);
		}
		
		mv.addObject("returnVal", returnVal);
		mv.addObject("type", type);		
		return mv;
	}
	
	@RequestMapping(value="/customer_excel.do")
	public void listsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_CUSTOMER sfmc_TB_CUSTOMER) throws Exception{
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_CUSTOMER> excelList = sfmcCustomerService.getCustomerList(sfmc_TB_CUSTOMER, true);
		resp.setHeader("Content-Disposition", "attachment; filename=customerList.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			HSSFCell headerC = rowHeader.createCell(2);
			HSSFCell headerD = rowHeader.createCell(3);
			HSSFCell headerE = rowHeader.createCell(4);
			headerA.setCellValue(new HSSFRichTextString("고객코드"));
			headerB.setCellValue(new HSSFRichTextString("업체명"));
			headerC.setCellValue(new HSSFRichTextString("전화번호"));
			headerD.setCellValue(new HSSFRichTextString("대표자"));
			headerE.setCellValue(new HSSFRichTextString("주소"));
			
			sfmc_TB_CUSTOMER = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i+1);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			HSSFCell cells3 = rowA.createCell(2);
			HSSFCell cells4 = rowA.createCell(3);
			HSSFCell cells5 = rowA.createCell(4);
			cells1.setCellValue(sfmc_TB_CUSTOMER.getCustCode());
			cells2.setCellValue(sfmc_TB_CUSTOMER.getCustName());
			cells3.setCellValue(sfmc_TB_CUSTOMER.getCustPhone());
			cells4.setCellValue(sfmc_TB_CUSTOMER.getCustBoss());
			cells5.setCellValue(sfmc_TB_CUSTOMER.getCustAddr());
		}
		
		try{
			workbook.write(ost);
			ost.flush();
			fos = new FileOutputStream(new File("C:/customerList.xls"));
			workbook.write(fos);
		}catch(Exception e){
			log.error(e);
		}finally{
			if(fos != null){
				try{
					fos.flush();
					fos.close();
				}
				catch(IOException e){
					log.error(e);
				}
			}
		}
	}
}
